package com.xuecheng.content.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.CommonError;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shiminghui
 * @date 2024/8/4 13:38
 * @description: TODO
 */
@Service
@RequiredArgsConstructor
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    private final CourseBaseInfoService courseBaseInfoService;

    /**
     * 根据课程id查询师资信息
     *
     * @param courseId
     * @return
     */
    @Override
    public List<CourseTeacherDto> getByCourseId(Long courseId) {
        if (courseId == null) {
            throw new XueChengPlusException("查询课程id为空");
        }
        List<CourseTeacher> list = lambdaQuery().eq(CourseTeacher::getCourseId, courseId)
                .list();
        List<CourseTeacherDto> collect = list.stream().map(entity -> BeanUtil.copyProperties(entity, CourseTeacherDto.class)).collect(Collectors.toList());
        return collect;
    }

    /**
     * 新增师资信息
     *
     * @param courseTeacherDto
     * @return
     */
    @Override
    public CourseTeacherDto saveCourseTeacher(CourseTeacherDto courseTeacherDto) {
        //首先判断是否有课程id
        if (courseTeacherDto.getCourseId() == null) {
            throw new XueChengPlusException("无课程id");
        }
        //判断课程是否存在
        CourseBase courseBase = courseBaseInfoService.getById(courseTeacherDto.getCourseId());
        if (courseBase == null) {
            throw new XueChengPlusException("课程不存在");
        }
        if (courseTeacherDto.getId() == null) {
            //添加
            CourseTeacher courseTeacher = BeanUtil.copyProperties(courseTeacherDto, CourseTeacher.class);
            courseTeacher.setCreateDate(LocalDateTime.now());
            boolean save = save(courseTeacher);
            if (!save) {
                throw new XueChengPlusException("师资信息添加失败");
            }
            BeanUtil.copyProperties(courseTeacher, courseTeacherDto);
        } else {
            //修改
            boolean b = updateById(courseTeacherDto);
            if (!b) {
                throw new XueChengPlusException("师资信息修改失败");
            }
        }
        return courseTeacherDto;
    }

    /**
     * 删除师资信息
     *
     * @param courseId
     * @param id
     */
    @Override
    public void deleteCourseTeacherById(Long courseId, Long id) {
        if (courseId == null || id == null) {
            throw new XueChengPlusException(CommonError.REQUEST_NULL.getErrMessage());
        }
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getCourseId, courseId)
                .eq(CourseTeacher::getId, id);
        boolean remove = remove(wrapper);
        if (!remove) {
            throw new XueChengPlusException("删除师资信息失败");
        }
    }
}
